package org.blogsomy.model.dao;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public abstract class HibernateDao<T> extends HibernateDaoSupport implements Dao<T> {

	private Class<T> type;

	public HibernateDao(Class<T> type) {
		this.type = type;
	}

	@Override
	public void delete(T entity) {
		getHibernateTemplate().delete(entity);
	}

	@SuppressWarnings("unchecked")
	@Override
	public T findById(Long id) {
		return (T) getHibernateTemplate().get(type, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> getAll() {
		String queryString = "from " + type.getSimpleName();
		return (List<T>) getHibernateTemplate().find(queryString);
	}

	@Override
	public Long save(T entity) {
		return (Long) getHibernateTemplate().save(entity);
	}

}
